<!-- 地区对照表编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改销毁客户':'添加销毁客户'"
    :visible="visible"
    width="460px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="120px">
      <el-form-item
        label="客户编码:"
        prop="name">
        <el-input
          :maxlength="20"
          v-model="form.customercode"
          placeholder=""
          clearable/>
      </el-form-item>
      <el-form-item
        label="客户名称:"
        prop="name">
        <el-input
          :maxlength="150"
          v-model="form.customername"
          placeholder=""
          clearable/>
      </el-form-item>
      <el-form-item
        label="客户地址:"
        prop="name">
        <el-input
          :maxlength="200"
          v-model="form.deliveryaddress"
          placeholder="请输入客户地址"
          clearable/>
      </el-form-item>
      <el-form-item
        label="地区:"
        prop="region">
        <el-select
          clearable
          v-model="form.region"
          size="small"
          placeholder="-请选择地区-"
          class="ele-block">
          <el-option v-for="item in regionList" :key="item" :label="item" :value="item"/>
        </el-select>
      </el-form-item>
      <el-form-item
        label="联系人:"
        prop="contacts">
        <el-input
          :maxlength="20"
          v-model="form.contacts"
          placeholder=""
          clearable/>
      </el-form-item>
      <el-form-item
        label="电话:"
        prop="tel">
        <el-input
          :maxlength="20"
          v-model="form.tel"
          placeholder=""
          clearable/>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'KdlAreaMapEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      regionList:[],
      // 表单验证规则
      rules: {
        custcode: [
          {required: true, message: '请输入客户编码', trigger: 'blur'}
        ],
        custname: [
          {required: true, message: '请输入客户名称', trigger: 'blur'}
        ],
        address: [
          {required: true, message: '请输入客户地址', trigger: 'blur'}
        ],
        region: [
          {required: true, message: '请选择地区', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  mounted() {
    this.getRegionList();
  },
  methods: {
    async getRegionList(){
      var res = await this.$http.get('/kdl/regionlist');
      if(res.status==200)
      {
        this.regionList = res.data.data;
      }

    },
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.form.customertype = "销毁客户";
          this.form.nation = "中国";
          this.form.transzone = "默认地区";
          this.form.profession = "经营企业";

          this.form.enname = this.form.deliveryaddress;
          this.$http[this.isUpdate ? 'post' : 'post'](this.isUpdate ? '/kdl/savecustomer' : '/kdl/savecustomer', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
.el-form-item {
  margin-bottom: 10px !important;
}
</style>
